<?php
namespace app\admin\controller;
use app\BaseController;
use think\facade\Cache;

use app\common\model\IconModel;
use app\common\model\IconCategoryModel;

class Icon extends BaseController{
    public function list(){
        $keyword = $this->request->param('keyword/s','');
        $category_id = $this->request->param('category_id/d',0);
        $where = [
            ['is_delete','=',0]
        ];
        if($category_id > 0){
            $where[] = ['category_id','=',$category_id];
        }
        if(!empty($keyword)){
            $where[] = ['icon_name','LIKE','%'.$keyword.'%'];
        }
        $list = IconModel::where($where)
            ->limit($this->request->limit)
            ->page($this->request->page)
            ->select();
        $total = IconModel::where($where)->count();
        return success([
            'list' => $list,
            'total' => $total
        ]);
    }
    
    public function save(){
        $IconModel = IconModel::where('update_time','<',strtotime('2024-09-12 12:00:00'))->find();
        $IconModel->icon_class = 'tn-'.$IconModel->icon_class;
        $IconModel->save();
        return success($IconModel);
        $data = $this->request->param('data/a','');
        $list = [];
        foreach ($data as $key => $value) {
            $list[$key] = [
                'category_id' => 20,
                'icon_name' => $value['description'],
                'icon_class' => 'icon-'.$value['icon']
            ];
        }
        $IconModel = new IconModel();
        // $IconModel->saveAll($list);
        return success($list);
        $dir = root_path().'/public/static/icon.js';
        $res = file_get_contents($dir);
        // $res = str_replace(array(" ", "\n", "\r"), "", $res);
        // $res = stripslashes($res);
        $res = json_decode($res,true);
        
        $icon_id = $this->request->param('icon_id/d',0);
        
        $category_id = $this->request->param('category_id/d',0);
        if(empty($category_id)){
            return error('分类错误');
        }
        $icon_name = $this->request->param('icon_name/s','');
        
        if(empty($icon_name)){
            return error('图标名称不能为空');
        }
        $icon_class = $this->request->param('icon_class/s','');
        if(empty($icon_class)){
            return error('图标类名称不能为空');
        }
        
        $sort = $this->request->param('sort/d',0);
        
        if($icon_id > 0){
            $IconModel = IconModel::find($icon_id);
        }else{
            $IconModel = new IconModel();
        }
        $IconModel->category_id = $category_id;
        $IconModel->icon_name = $icon_name;
        $IconModel->icon_class = $icon_class;
        $IconModel->sort = $sort;
        
        if($IconModel->save()){
            return success('添加成功');
        }else{
            return error('添加失败');
        }
    }
    
    public function categoryList(){
        $list = IconCategoryModel::where('is_delete',0)->select();
        return success([
            'list' => $list
        ]);
    }
    // public function categorySave(){
    //     $category_id = $this->request->param('category_id/d',0);
    //     $category_name = $this->request->param('category_name/s','');
    //     if(empty($category_name)){
    //         return error('分类名称不能为空');
    //     }
    //     $sort = $this->request->param('sort/d',0);
        
    //     if($category_id > 0){
    //         $IconCategoryModel = IconCategoryModel::find($category_id);
    //     }else{
    //         $IconCategoryModel = new IconCategoryModel();
    //     }
    //     $IconCategoryModel->category_name = $category_name;
    //     $IconCategoryModel->sort = $sort;
        
    //     if($IconCategoryModel->save()){
    //         return success('添加成功');
    //     }else{
    //         return error('添加失败');
    //     }
    // }
}